﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Microsoft.Practices.Unity;
using Marking.Dashboard.Infrastructures;
using Marking.Dashboard.Services;

namespace Marking.Dashboard.Views.Login
{
    public partial class LoginWindow : ChildWindow
    {
        private Action _success;
        private Action _failure;

        public LoginWindow(Action success)
        {
            InitializeComponent();

            _success = success;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.Login();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void ChildWindow_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.Login();
                return;
            }

            if (e.Key == Key.Escape)
            {
                this.DialogResult = false;
            }
        }

        private void Login()
        {
            var loginService = UnityHelper.Container.Resolve<ILoginService>();

            loginService.Login(this.Password.Password,
                () =>
                {
                    this.DialogResult = true;

                    _success();
                },
                () =>
                {
                    this.Message.Text = "密码错误！";
                });
        }
    }
}

